package com.tanhua.manager.listener;

import com.itheima.commons.template.ContentCensorTemplate;
import com.itheima.domain.mongo.Publish;
import com.itheima.dubbo.api.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 阿鹏
 * @date 2021/2/23 23:08
 */
@Component
@RocketMQMessageListener(topic = "tanhua-publish9", consumerGroup = "tanhua-publish-consumer9")
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private ContentCensorTemplate contentCensorTemplate;

    @Override
    public void onMessage(String message) {
        Publish publish = publishApi.findById(message);

        if (publish.getState() == 0) {

            boolean textOk = contentCensorTemplate.textCensor(publish.getTextContent());
            boolean imageOk = contentCensorTemplate.imageBatchCensor(publish.getMedias());

            publish.setState(textOk && imageOk ? 1 : 2);

            publishApi.updateState(publish);
        }
    }
}
